import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const routes = [
 
  {
    path: '/about',
    name: 'about',
   
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  }, {
    path: '/index',
    name: 'index',
   
    component: () => import(/* webpackChunkName: "about" */ '../views/Index.vue'),
    children:[ {
        path: '/home',
        name: 'home',
        component: HomeView,
        // beforeEnter: (to, from,next) => {
        //      if(localStorage.getItem('token'))  {
        //         // 放行
        //         return true

        //      }else{
        //         // 守卫
        //         // return false
        //         next('/login')

        //      }
            
        // }
      },]
  },
  {
    path: '/login',
    name: 'login',
   
    component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue'),
    beforeEnter: (to, from,next) => {
        if(!localStorage.getItem('token'))  {
           // 放行
           next()

        }else{
           // 守卫
           // return false
           next('/home')

        }
       
   }
    
    
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

export default router
